package com.gzhryc.shared_device.oem.h5.mng.controllers;

import com.gzhryc.common.data.dto.SelectResult;
import com.gzhryc.common.data.dto.TreeResult;
import com.gzhryc.dictionary.dao.db.CountryArea;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.CountryAreaUtils;
import com.gzhryc.shared_device.oem.code.config.dao.db.LogisticsCompany;
import com.gzhryc.shared_device.oem.code.config.dao.enums.LogisticsCompanyEnum;
import com.gzhryc.shared_device.oem.code.config.services.LogisticsCompanyService;
import com.gzhryc.shared_device.oem.code.config.services.dto.LogisticsCompanySearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class CommonController {

    /**
     * 获取中国地区树结构
     *
     * @param request
     * @param response
     * @return
     */
    @Route("/common/china/area/tree/select.data")
    public TreeResult<CountryArea> select(HttpServletRequest request, HttpServletResponse response) {
        TreeResult<CountryArea> result = new TreeResult<CountryArea>(DtoConstants.SUCCESS);
        List<CountryArea> data = CountryAreaUtils.getCountryAreaList();
        result.getBody().setData(data);
        return result;
    }

    @Route("/common/logistics_company/select.data")
    public SelectResult selectData(HttpServletRequest request) {
        SelectResult result = new SelectResult(DtoConstants.SUCCESS);
        String term = request.getParameter("term");
        Integer page = WebUtils.getInteger(request, "page", 1);

        LogisticsCompanySearch search = new LogisticsCompanySearch();
        search.setName(term);
        search.setMaxState(LogisticsCompanyEnum.State.Default.index());
        Long total = 0L;
        List<LogisticsCompany> list = LogisticsCompanyService.self().findBySearch(search, page, WebConstants.PAGE_LIMIT);
        if (list != null) {
            for (LogisticsCompany logisticsCompany : list) {
                SelectResult.Item item = new SelectResult.Item();
                item.setId(logisticsCompany.getId());
                item.setText(logisticsCompany.getName());
                result.getBody().getData().add(item);
            }
            total += list.size();
        }
        result.getBody().setPage(page);
        result.getBody().setLimit(WebConstants.PAGE_LIMIT);
        result.getBody().setTotal(total);
        return result;
    }

}
